#import "SSDiskInfo.h"
#define MB (1024*1024)
#define GB (MB*1024)
@implementation SSDiskInfo
+ (NSString *)diskSpace {
	@try {
        long long space = [self longDiskSpace];
        if (space <= 0) {
            return nil;
        }
        NSString *diskSpace = [self formatMemory:space];
        if (diskSpace == nil || diskSpace.length <= 0) {
            return nil;
        }
        return diskSpace;
	}
	@catch (NSException * ex) {
        return nil;
	}
}
+ (NSString *)freeDiskSpace:(BOOL)inPercent {
	@try {
        long long Space = [self longFreeDiskSpace];
        if (Space <= 0) {
            return nil;
        }
        NSString *diskSpace;
        if (inPercent) {
            long long TotalSpace = [self longDiskSpace];
            float PercentDiskSpace = (Space * 100) / TotalSpace;
            if (PercentDiskSpace <= 0) {
                return nil;
            }
            diskSpace = [NSString stringWithFormat:@"%.f%%", PercentDiskSpace];
        } else {
            diskSpace = [self formatMemory:Space];
        }
        if (diskSpace == nil || diskSpace.length <= 0) {
            return nil;
        }
        return diskSpace;
	}
	@catch (NSException * ex) {
        return nil;
	}
}
+ (NSString *)usedDiskSpace:(BOOL)inPercent {
    @try {
        long long uds;
        long long tds = [self longDiskSpace];
        long long fds = [self longFreeDiskSpace];
        if (tds <= 0 || fds <= 0) {
            return nil;
        }
        uds = tds - fds;
        if (uds <= 0) {
            return nil;
        }
        NSString *usedDiskSpace;
        if (inPercent) {
            float percentUsedDiskSpace = (uds * 100) / tds;
            if (percentUsedDiskSpace <= 0) {
                return nil;
            }
            usedDiskSpace = [NSString stringWithFormat:@"%.f%%", percentUsedDiskSpace];
        } else {
            usedDiskSpace = [self formatMemory:uds];
        }
        if (usedDiskSpace == nil || usedDiskSpace.length <= 0) {
            return nil;
        }
        return usedDiskSpace;
    }
    @catch (NSException *exception) {
        return nil;
    }
}
#pragma mark - Disk Information Long Values
+ (long long)longDiskSpace {
	@try {
        long long diskSpace = 0L;
		NSError *error = nil;
        NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
        if (error == nil) {
            diskSpace = [[fileAttributes objectForKey:NSFileSystemSize] longLongValue];
        } else {
            return -1;
        }
        if (diskSpace <= 0) {
            return -1;
        }
        return diskSpace;
	}
	@catch (NSException *exception) {
        return -1;
	}
}
+ (long long)longFreeDiskSpace {
	@try {
        long long FreeDiskSpace = 0L;
		NSError *error = nil;
        NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
        if (error == nil) {
            FreeDiskSpace = [[fileAttributes objectForKey:NSFileSystemFreeSize] longLongValue];
        } else {
            return -1;
        }
        if (FreeDiskSpace <= 0) {
            return -1;
        }
        return FreeDiskSpace;
	}
	@catch (NSException *exception) {
        return -1;
	}
}
#pragma mark - Memory Value Formatting
+ (NSString *)formatMemory:(long long)Space {
	@try {
        NSString *formattedBytes = nil;
		double numberBytes = 1.0 * Space;
		double totalGB = numberBytes / GB;
		double totalMB = numberBytes / MB;
		if (totalGB >= 1.0) {
            formattedBytes = [NSString stringWithFormat:@"%.2f GB", totalGB];
	    } else if (totalMB >= 1)
            formattedBytes = [NSString stringWithFormat:@"%.2f MB", totalMB];
		else {
            formattedBytes = [self formattedMemory:Space];
            formattedBytes = [formattedBytes stringByAppendingString:@" bytes"];
		}
        if (formattedBytes == nil || formattedBytes.length <= 0) {
            return nil;
        }
        return formattedBytes;
	}
	@catch (NSException *exception) {
        return nil;
	}
}
+ (NSString *)formattedMemory:(unsigned long long)Space {
	@try {
        NSString *formattedBytes = nil;
		NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
		[formatter setPositiveFormat:@"###,###,###,###"];
		NSNumber *theNumber = [NSNumber numberWithLongLong:Space];
		formattedBytes = [formatter stringFromNumber:theNumber];
        if (formattedBytes == nil || formattedBytes.length <= 0) {
            return nil;
        }
        return formattedBytes;
	}
	@catch (NSException *exception) {
        return nil;
	}
}
@end
